package com.william.jdbcplus.core.jdbc.handle.impl;

import com.william.jdbcplus.core.jdbc.handle.RsHandler;

import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author niliwei
 */
public class ListHandler implements RsHandler<List<Map<String, Object>>> {
    private static ListHandler instance = new ListHandler();

    private ListHandler() {
    }

    public static ListHandler getInstance() {
        return instance;
    }

    @Override
    public List<Map<String, Object>> handle(ResultSet rs) throws SQLException {
        List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
        ResultSetMetaData md = rs.getMetaData();
        int columnCount = md.getColumnCount();
        while (rs.next()) {
            Map<String, Object> rowData = new HashMap<String, Object>();
            for (int i = 1; i <= columnCount; i++) {
                rowData.put(md.getColumnName(i), rs.getObject(i));
            }
            list.add(rowData);
        }
        return list;
    }
}
